package com.xbongbong.saas.help;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.pro.productwarehouse.pojo.dto.ProductWareHouseUpdateDTO;
import com.xbongbong.saas.domain.entity.ProductWarehouseEntity;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/4/5 16:11
 * @since v1.0
 */
@Component
public class ProductWarehouseHelp {


    /**
     * 获取产品仓库更新的库存和成本
     * @author 吴峰
     * @param productWarehouse 产品库存
     * @param productCost 产品成本
     * @param changeNum
     * @param amountPrecision “金额”的小数精度
     * @param costVisible 判断产品成本字段是否启用 ture：启用 false 不启用
     * @param type 1 加 2 减
     * @return
     * @throws XbbException
     */
    public static ProductWareHouseUpdateDTO getProductWarehouseUpdate(ProductWarehouseEntity productWarehouse, Double productCost,
                                                                      Double changeNum, Integer amountPrecision, Boolean costVisible, Integer type) throws XbbException {
        Double productWarehouseCost = productWarehouse.getCost();
        Double productWarehouseNum = productWarehouse.getNum();
        ProductWareHouseUpdateDTO productWareHouseUpdateDTO = new ProductWareHouseUpdateDTO();
        if (costVisible) {
            Double cost = SaasProductHelp.calculateCost(productWarehouseNum, productWarehouseCost, changeNum, productCost, type);
            cost = Arith.round(cost, amountPrecision);
            productWareHouseUpdateDTO.setCost(cost);
        }
        Double num;
        if (Objects.equals(type, 1)) {
            num = Arith.add(productWarehouseNum, changeNum);
        } else {
            num = Arith.sub(productWarehouseNum, changeNum);
        }
        productWareHouseUpdateDTO.setNum(num);
        productWareHouseUpdateDTO.setId(productWarehouse.getId());
        return productWareHouseUpdateDTO;

    }
}
